//
// $Id: OverlayWindow.m 6 2010-08-05 19:43:27Z principiadriver $
//
// Written by
//  Michael Klein <michael.klein@puffin.lb.shuttle.de>
//
// This file is part of Alternate Moves.
//
// This  is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#import "OverlayWindow.h"


@implementation OverlayWindow


-(id)initWithContentRect:(NSRect)contentRect
               styleMask:(unsigned)windowStyle
                 backing:(NSBackingStoreType)bufferingType
                   defer:(BOOL)deferCreation
{
    if ((self = [super initWithContentRect:contentRect
                                 styleMask:NSBorderlessWindowMask
                                   backing:bufferingType
                                     defer:deferCreation]) != nil) {
        [self setOpaque:NO];
        [self setBackgroundColor:[NSColor clearColor]];
        [self setLevel:NSFloatingWindowLevel];
        [self setIgnoresMouseEvents:YES];
        
        fadeTimer = nil;
    }
    return self;
}


- (IBAction)orderFront:(id)sender
{
    [fadeTimer invalidate];
    fadeTimer = nil;
    return [super orderFront:sender];
}


- (IBAction)fade:(id)sender
{
    fadeTimer = [NSTimer scheduledTimerWithTimeInterval:0.04
                                                 target:self
                                               selector:@selector(fadeTimerFired:)
                                               userInfo:nil
                                                repeats:YES];
}


- (void)fadeTimerFired:(NSTimer*)t
{
    float alpha = [self alphaValue];
    if (alpha <= 0.2) {
        [fadeTimer invalidate];
        fadeTimer = nil;
        [self orderOut:self];
    } else {
        [self setAlphaValue:alpha-0.2];
    }
}


@end
